package com.jtyoui.hdfs;

import java.io.IOException;

import com.jtyoui.path.Address;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

public class FindFileLocation {

    public static void main(String[] args) throws IOException {
        System.setProperty("HADOOP_USER_NAME", "hadoop");
        Configuration conf = new Configuration();
        FileSystem fs = FileSystem.get(conf);
        Path path = new Path(Address.HADOOP_PATH.toString() + "/write.txt");
        FileStatus status = fs.getFileStatus(path);
        BlockLocation[] locations = fs.getFileBlockLocations(status, 0, status.getLen());
        int length = locations.length;
        for (int i = 0; i < length; i++) {
            String[] hosts = locations[i].getHosts();
            System.out.println("block_" + i + "_location:" + hosts[i]);
        }
    }

}
